// Tags:
#include <cstdio>
#include <stack>

const int N = 1e2 + 5;
int tot, cnt, n, m, dfn[N], low[N], scc[N], in[N], f[N][505];
bool ins[N];
std::stack<int> stk;

struct Edge {
  int v, nxt;
};

struct Graph {
  int e_cnt, heads[N], w[N], v[N];
  Edge e[N << 1];
  inline void add(int u, int v) {
    e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
  }
} E, G;

struct DGraph : Graph {
  inline void add(int u, int v) {
    printf("%d %d\n", u, v);
    e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
  }
};

void tarjan(int x) {
  dfn[x] = low[x] = ++cnt;
  stk.push(x);
  ins[x] = true;
  for (int i = E.heads[x], v; i; i = E.e[i].nxt) {
    if (!dfn[v = E.e[i].v]) {
      tarjan(v);
      low[x] = std::min(low[x], low[v]);
    } else if (ins[v])
      low[x] = std::min(low[x], dfn[v]);
  }
  if (low[x] >= dfn[x]) {
    int tmp;
    ++tot;
    do {
      tmp = stk.top();
      stk.pop();
      ins[tmp] = false;
      scc[tmp] = tot;
    } while (tmp != x);
  }
}

void solve(int x) {
  for (int i = G.w[x]; i <= m; ++i) f[x][i] = G.v[x];
  for (int i = G.heads[x], v; i; i = G.e[i].nxt) {
    solve(v = G.e[i].v);
    for (int j = m; j >= G.w[x]; --j) {
      for (int k = 0; k <= j - G.w[x]; ++k) {
        f[x][j] = std::max(f[x][j], f[v][k] + f[x][j - k]);
      }
    }
  }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("P2515 [HAOI2010]软件安装.in", "r", stdin);
  freopen("P2515 [HAOI2010]软件安装.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &E.w[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &E.v[i]);
  for (int i = 1, d; i <= n; ++i) {
    scanf("%d", &d);
    if (d) E.add(d, i);
  }
  for (int i = 1; i <= n; ++i)
    if (!dfn[i]) tarjan(i);
  for (int i = 1; i <= n; ++i) {
    G.w[scc[i]] += E.w[i], G.v[scc[i]] += E.v[i];
    for (int j = E.heads[i], v; j; j = E.e[j].nxt) {
      if (scc[i] != scc[v = E.e[j].v]) { G.add(scc[i], scc[v]), in[scc[v]]++; }
    }
  }
  for (int i = 1; i <= tot; ++i)
    if (in[i] == 0) G.add(0, i);
  solve(0);
  int ans = 0;
  for (int i = 1; i <= m; ++i) ans = std::max(ans, f[0][i]);
  printf("%d", f[0][m]);
  return 0;
}